<!-- 新增编辑车辆 -->
<template>
  <el-dialog
    :visible="isShowAdd"
    title="新增车辆"
    width="40%"
    @close="onClose"
  >
    <el-form
      ref="truckFromRef"
      :model="truckFrom"
      :rules="truckFromRules"
      label-width="120px"
    >
      <el-form-item
        label="车辆类型"
        prop="truckTypeName"
      >
        <el-select
          v-model="truckFrom.truckTypeId"
          placeholder="请选择"
        >
          <el-option
            v-for="(item,index) in tableData"
            :key="index"
            :label="item.truckTypeName"
            :value="item.truckTypeId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="车牌号码"
        prop="licensePlate"
      >
        <el-input
          v-model="truckFrom.licensePlate"
          placeholder="请输入车牌号码"
        />
      </el-form-item>
      <el-form-item
        label="GPS设备ID"
        prop="deviceGpsId"
      >
        <el-input
          v-model="truckFrom.deviceGpsId"
          placeholder="请输入GPS设备ID"
        />
      </el-form-item>
    </el-form>
    <!-- 按钮 -->
    <template #footer>
      <el-row
        type="flex"
        justify="center"
      >
        <el-button
          type="danger"
          @click="submit"
        >确定</el-button>
        <el-button
          type="info"
          @click="onClose"
        >取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { truckList, truckTypeAdd } from '@/api/transit'
export default {
  name: 'VehicleAdd',
  // 父传子数据
  props: {
    isShowAdd: {
      type: Boolean,
      default: false
    }
  },
  data() {
    // 车牌号码校验
    const carPhoneValid = (rule, value, callback) => {
      const plateNumber = /^([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1})$/
      if (plateNumber.test(value)) {
        callback()
      } else {
        callback(new Error('请输入正确的车牌号'))
      }
    }
    return {
      // 需要的数据
      truckFrom: {
        allowableLoad: 0,
        allowableVolume: 0,
        brand: '',
        deviceGpsId: '',
        driverName: '',
        driverNum: 0,
        id: 0,
        licensePlate: '',
        loadingRatio: 0,
        picture: '',
        runStatus: 0,
        status: 0,
        transportLineName: '',
        truckLicenseId: 0,
        truckTypeId: '',
        truckTypeName: '',
        workStatus: 0
      },
      // 表格校验规则
      truckFromRules: {
        licensePlate: [
          {
            required: true,
            message: '车牌号码不能为空',
            trigger: 'blur'
          },
          { validator: carPhoneValid, trigger: 'blur' }
        ],
        deviceGpsId: [{
          required: true,
          message: 'GPS设备ID不能为空',
          trigger: 'blur'
        }],
        truckTypeName: [{
          required: true,
          message: '车辆类型不能为空',
          trigger: 'blur'
        }]
      },
      dataForm: {
        page: 1,
        pageSize: 10
      },
      tableData: []
    }
  },
  mounted() {
    this.getlist()
  },
  methods: {
    // 获取页面数据
    async getlist() {
      const { data } = await truckList(this.dataForm)
      this.tableData = data.items
      // 数组去重
      const map = new Map()
      for (const item of this.tableData) {
        if (!map.has(item.truckTypeId)) {
          map.set(item.truckTypeId, item)
        }
      }
      this.tableData = [...map.values()]
    },
    // 关闭按钮
    onClose() {
      this.truckFrom = {
        allowableLoad: 0,
        allowableVolume: 0,
        brand: '',
        deviceGpsId: '',
        driverName: '',
        driverNum: 0,
        id: 0,
        licensePlate: '',
        loadingRatio: 0,
        picture: '',
        runStatus: 0,
        status: 0,
        transportLineName: '',
        truckLicenseId: 0,
        truckTypeId: '',
        truckTypeName: '',
        workStatus: 0
      }
      this.$refs.truckFromRef.resetFields()
      this.$emit('update:isShowAdd', false)
    },
    // 新增车辆提交
    async submit() {
      try {
        await truckTypeAdd(this.truckFrom)
        this.$message.success('添加车辆成功')
        this.onClose()
        // 渲染页面
        this.$emit('update-truck')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
<style scoped lang="scss">
/deep/ .el-dialog__body {
  padding: 40px 50px 0 20px !important;
}
</style>
